<?php


namespace app\api\controller;


use think\exception\DbException;
use think\facade\Request;

class Product extends Common
{
    /**
     * @param Request $request
     * @param \app\api\model\Product $model
     * @return \think\response\Json
     */
    public function index(Request $request, \app\api\model\Product $model)
    {
        $where = [];
        if ($request::has('commodity')) {
            $where['commodity_id'] = $request::param('commodity');
        }
        return $this->returnJson(200, 'ok',
            $model->index($where, $request::param('page'), $request::param('size'))
        );
    }

    /**
     * @param $id
     * @param \app\api\model\Product $model
     * @return \think\response\Json
     */
    public function show($id, \app\api\model\Product $model)
    {
        return $this->returnJson(200, 'ok', $model->info($id));
    }

    /**
     * @param Request $request
     * @param \app\api\model\Product $model
     * @return \think\response\Json
     */
    public function store(Request $request,\app\api\model\Product $model)
    {
        return $this->returnJson(200, 'ok', $model->add($request::param()));
    }

    /**
     * @param $id
     * @param Request $request
     * @param \app\api\model\Product $model
     * @return \think\response\Json
     */
    public function update($id, Request $request, \app\api\model\Product $model)
    {
        $res = $model->edit($id, $request::param());
        return is_string($res) ? $this->returnJson(500, $res) : $this->returnJson(200, 'ok', $res);
    }

    /**
     * @param $id
     * @param \app\api\model\Product $model
     * @return \think\response\Json
     */
    public function delete($id, \app\api\model\Product $model)
    {
        $res = $model->del($id);
        return is_string($res) ? $this->returnJson(500, $res) : $this->returnJson(200, 'ok', $res);
    }

    public function getAllProduct($id,\app\api\model\Product $model)
    {
        return $this->returnJson(200,'ok',$model->getAllProduct($id));
    }
}
